package com.csust.automaticteach.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.model.dto.practicesituation.PracticeSituationQueryForExamsRequest;
import com.csust.automaticteach.model.dto.practicesituation.PracticeSituationUpdateRequest;
import com.csust.automaticteach.model.entity.PracticeSituation;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.generateexam.GenerateExamPageVO;
import com.csust.automaticteach.model.vo.generateexam.GenerateExamVO;
import com.csust.automaticteach.model.vo.practicesituation.PracticeSituationVO;
import com.csust.automaticteach.service.ClassesService;
import com.csust.automaticteach.service.PracticeSituationService;
import com.csust.automaticteach.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassNamePracticeSituationController
 * @Description 学生练习情况相关接口
 * @Author钰玟
 * @Date2025/7/12 下午5:21
 * @Version 1.0
 **/
@RestController
@RequestMapping("/practiceSituation")
public class PracticeSituationController {

    @Resource
    private PracticeSituationService practiceSituationService;
    @Resource
    private UserService userService;



    @GetMapping("/get")
    public BaseResponse<PracticeSituationVO> getPracticeSituationVO(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        //构造wrapper
        QueryWrapper<PracticeSituation> practiceSituationQueryWrapper = new QueryWrapper<>();
        practiceSituationQueryWrapper.eq("userId", loginUser.getId());
        PracticeSituation practiceSituation = practiceSituationService.getOne(practiceSituationQueryWrapper);
        //不为空
        PracticeSituationVO practiceSituationVO = new PracticeSituationVO();
        if (practiceSituation != null) {
            practiceSituationVO = practiceSituationService.getPracticeSituationVO(practiceSituation);
        } else {
            //为空则
            practiceSituationVO = practiceSituationService.addPracticeSituation(loginUser);
        }

        return ResultUtils.success(practiceSituationVO);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updatePracticeSituation(@RequestBody PracticeSituationUpdateRequest practiceSituationUpdateRequest) {
        return ResultUtils.success(practiceSituationService.updatePracticeSituation(practiceSituationUpdateRequest));
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePracticeSituation(@RequestBody DeleteRequest deleteRequest) {
        return ResultUtils.success(practiceSituationService.removeById(deleteRequest.getId()));
    }


    /**
     * 获取该学生 所在的班级发布的练习
     * @param request
     * @return
     */
    @PostMapping("/get/exams")
    public BaseResponse<Page<GenerateExamPageVO>> getExams(@RequestBody PracticeSituationQueryForExamsRequest queryForExamsRequest
            , HttpServletRequest request) {
        int current = queryForExamsRequest.getCurrent();
        int pageSize = queryForExamsRequest.getPageSize();

        //获取当前用户
        User loginUser = userService.getLoginUser(request);
        //查询得到 当前用户所在的班级id
        QueryWrapper<PracticeSituation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", loginUser.getId());
        PracticeSituation situation = practiceSituationService.getOne(queryWrapper);
        Long classId = situation.getClassId();
        List<GenerateExamPageVO> examsByClassIdWithStudent = practiceSituationService.getExamsByClassIdWithStudent(classId);
        Page<GenerateExamPageVO> generateExamPageVOPage = new Page<>(current, pageSize, examsByClassIdWithStudent.size());
        generateExamPageVOPage.setRecords(examsByClassIdWithStudent);
        return ResultUtils.success(generateExamPageVOPage);
    }

}
